#region

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

#endregion

namespace SharePoint403PageFeature
{
    public class FeatureReceiver : SPFeatureReceiver
    {
        private const string HttpModuleName = "403Module";
        private static readonly SPWebConfigModification[] Modifications = {
                                                                              new SPWebConfigModification
                                                                                  {
                                                                                      Owner = HttpModuleName,
                                                                                      Name = "add[@name='SharePoint403PageFeature']",
                                                                                      Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
                                                                                      Path = "configuration/system.web/httpModules",
                                                                                      Sequence = 0,
                                                                                      Value = "<add name='SharePoint403PageFeature' type='SharePoint403Module.Custom403PageModule, SharePoint403PageFeature, Version=1.0.0.0, Culture=neutral, PublicKeyToken=94efd8419d3f51f4' />"
                                                                                  },

                                                                                  new SPWebConfigModification
                                                                                  {
                                                                                      Owner = HttpModuleName,
                                                                                      Name = "add[@key='TotalDigits']", 
                                                                                      Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
                                                                                      Path = "configuration/appSettings",
                                                                                      Sequence = 0,
                                                                                      Value = "<add key=\"SPCustom403PageUrl\" value=\"#\" />"
                                                                                  }
                                                                          };

 
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            var webApp = properties.Feature.Parent as SPWebApplication;
            if (webApp != null)
            {
                AddWebConfigModifications(webApp, Modifications);
            }
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            var webApp = properties.Feature.Parent as SPWebApplication;
            if (webApp != null)
            {
                RemoveWebConfigModificationsByOwner(webApp, HttpModuleName);
            }
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
        }

        private static void AddWebConfigModifications(SPWebApplication webApp, IEnumerable<SPWebConfigModification> modifications)
        {
            foreach (SPWebConfigModification modification in modifications)
            {
                webApp.WebConfigModifications.Add(modification);
            }

            webApp.Update();
            webApp.WebService.ApplyWebConfigModifications();
        }

        private static void RemoveWebConfigModificationsByOwner(SPWebApplication webApp, string owner)
        {
            var modifications = webApp.WebConfigModifications;
            var removeCollection = new Collection<SPWebConfigModification>();

            int count = modifications.Count;
            for (int i = 0; i < count; i++)
            {
                var modification = modifications[i];
                if (modification.Owner == owner)
                {
                    removeCollection.Add(modification);
                }
            }

            // now delete the modifications from the web application
            if (removeCollection.Count > 0)
            {
                foreach (var modificationItem in removeCollection)
                {
                    webApp.WebConfigModifications.Remove(modificationItem);
                }

                webApp.Update();
                webApp.WebService.ApplyWebConfigModifications();
            }
        }
    }
}